//
//  FDGuideController.m
//  FastDoctor
//
//  Created by 程建良 on 16/7/11.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "FDGuideController.h"
#import "FDGuideCell.h"

@interface FDGuideController ()
/**
 *  存放所有背景图片的数组
 */
@property (nonatomic, strong) NSArray<UIImage *> *imagesArr;

/**
 *  篮球,足球
 */
@property (nonatomic, weak) UIImageView *ballView;
/**
 *  大文字
 */
@property (nonatomic, weak) UIImageView *largeTextView;
/**
 *  小文字
 */
@property (nonatomic, weak) UIImageView *smallTextView;

@end

@implementation FDGuideController
static NSString * const reuseIdentifier = @"Cell";
#pragma mark - 重写init方法,指定布局
-(instancetype)init
{
    //创建流水布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置cell的大小
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    //设置水平方向滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置item的间距
    flowLayout.minimumLineSpacing = 0;
    return [super initWithCollectionViewLayout:flowLayout];
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    //设置基本属性
    self.collectionView.backgroundColor = kThemeColor;
    //关闭弹簧效果
    self.collectionView.bounces = NO;
    //设置分页效果
    self.collectionView.pagingEnabled = YES;
    //关闭底部的滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //注册cell
    [self.collectionView registerClass:[FDGuideCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark - 添加图片框的操作!
-(void)setupUI
{
    //篮球足球
    UIImageView *ballView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide1"]];
    ballView.frame = CGRectMake((self.collectionView.frame.size.width - ballView.frame.size.width)*0.5, ballView.frame.origin.y, ballView.frame.size.width, ballView.frame.size.height);
    [self.collectionView addSubview:ballView];
    
    
    //大文字
    UIImageView *largeTextView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largeTextView.frame = CGRectMake((self.collectionView.frame.size.width - largeTextView.frame.size.width)*0.5, self.collectionView.frame.size.height*0.7, largeTextView.frame.size.width, largeTextView.frame.size.height);
    
    [self.collectionView addSubview:largeTextView];
    
    
    //小文字
    UIImageView *smallTextView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallTextView.frame = CGRectMake((self.collectionView.frame.size.width - smallTextView.frame.size.width)*0.5, self.collectionView.frame.size.height*0.8, smallTextView.frame.size.width, smallTextView.frame.size.height);
    [self.collectionView addSubview:smallTextView];
    self.ballView = ballView;
    self.largeTextView = largeTextView;
    self.smallTextView = smallTextView;
 
}

#pragma mark - 代理方法
// 当用户抬起手指,scrollView开始变慢[减速],减速完成[静止]的时候调用一次!
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    /*
    // 1.获取在屏幕x轴方向的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 2.切换图片
    // 2.0 计算索引
    CGFloat idx = offsetX / scrollView.frame.size.width;
    
    // 2.1 拼接图片名称
    NSString *ballName = [NSString stringWithFormat:@"guide%@", @(idx + 1)];
    NSString *largeTextName = [NSString stringWithFormat:@"guideLargeText%@", @(idx + 1)];
    NSString *smallTextName = [NSString stringWithFormat:@"guideSmallText%@", @(idx + 1)];
    
    // 2.2 改图片
    self.ballView.image = [UIImage imageNamed:ballName];
    self.largeTextView.image = [UIImage imageNamed:largeTextName];
    self.smallTextView.image = [UIImage imageNamed:smallTextName];
    
    self.ballView.frame = CGRectMake(offsetX+(self.collectionView.frame.size.width - self.ballView.frame.size.width)*0.5, self.ballView.frame.origin.y, self.ballView.frame.size.width, self.ballView.frame.size.height);
    self.largeTextView.frame = CGRectMake(offsetX+(self.collectionView.frame.size.width - self.largeTextView.frame.size.width)*0.5, self.largeTextView.frame.origin.y, self.largeTextView.frame.size.width, self.largeTextView.frame.size.height);
    self.smallTextView.frame = CGRectMake(offsetX+(self.collectionView.frame.size.width - self.smallTextView.frame.size.width)*0.5, self.smallTextView.frame.origin.y, self.smallTextView.frame.size.width, self.smallTextView.frame.size.height);
    */
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.imagesArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FDGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // 设置背景图片
    cell.backgroundView = [[UIImageView alloc] initWithImage:self.imagesArr[indexPath.item]];
    
    // 最后一个cell现实按钮,其他的隐藏!
    if (indexPath.item == self.imagesArr.count - 1)
    {
        cell.experienceBtn.hidden = NO;
    }
    else
    {
        
        cell.experienceBtn.hidden = YES;
    }
    
    return cell;
}


#pragma mark - 懒加载
- (NSArray<UIImage *> *)imagesArr
{
    
    if (_imagesArr == nil)
    {
        /*
        _imagesArr = @[
                       [UIImage imageNamed:@"guide1Background"],
                       [UIImage imageNamed:@"guide2Background"],
                       [UIImage imageNamed:@"guide3Background"],
                       [UIImage imageNamed:@"guide4Background"],
                       ];
        */
        _imagesArr = @[
                       [UIImage imageNamed:@"g1"],
                       [UIImage imageNamed:@"g2"],
                       [UIImage imageNamed:@"g3"],
                       [UIImage imageNamed:@"g4"],
                       ];
        
    }
    return _imagesArr;
    
}


-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    //[self setupUI];
}
@end
